﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;
using System.Data;
using System.Data.Entity.Validation;
using System.Data.Entity.Infrastructure;


namespace BLLA
{
    public partial class Order_Send_T : BaseBLL<Model.Order_Send_T>, IBLL.IOrder_Send_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        ResultSend RS = new ResultSend();


        /// <summary>
        /// 根据登陆的操作员获取所属的调度单
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="ConID">订单状态ConID(多选时用“,”分隔)</param>
        /// <param name="StartTime">开始时间（""-全部/yyyyMMdd）</param>
        /// <param name="EndTime">结束时间（""-全部/yyyyMMdd）</param>
        /// <param name="FormID">订单ID（""-全部）</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetOrder_SendByOperId(int OperID, string ConID, string StartTime, string EndTime, string FormID, int pageIndex, int pageSize, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            List<Model.View_Order_Form> mo = new List<Model.View_Order_Form>();
            try
            {
                List<Model.Order_Oper_T> operList = base.DBSession.IOrder_Oper_TDAL.GetListBy(u => u.OperID == OperID);
                if (operList.Count <= 0)
                {
                    result.BS = "-1";
                    result.Msg = "找不到此操作员ID！";
                    return os.StringOfObject(result, RETID);
                }

                Model.Order_Oper_T oper = operList.First();

                //   var predicate = PredicateBuilder.True<Model.Order_Form_T>();
                StringBuilder sb = new StringBuilder();

                string[] operArr = oper.MerID.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (operArr.Length <= 0)
                {
                    result.BS = "-0";
                    result.Msg = "当前操作员无可操作商户！";
                    return os.StringOfObject(result, RETID);
                }
                sb.Append("AND  FormMerID in ('" + operArr[0] + "'");
                for (int i = 1; i < operArr.Length; i++)
                {
                    sb.Append(" ,'" + operArr[i] + "'");
                }
                sb.Append(") ");

                if (!string.IsNullOrWhiteSpace(ConID))
                {
                    string[] conArr = ConID.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    sb.Append(" AND FormBY in ( '" + conArr[0] + "' ");
                    for (int i = 1; i < conArr.Length; i++)
                    {
                        sb.Append(",'" + conArr[i] + "' ");
                    }
                    sb.Append(")");
                }
                if (!string.IsNullOrWhiteSpace(StartTime))
                {
                    sb.Append(" AND LastTime>= " + StartTime + "000000 ");
                }
                if (!string.IsNullOrWhiteSpace(EndTime))
                {
                    sb.Append(" AND LastTime<=" + EndTime + "235959 ");
                }
                if (!string.IsNullOrWhiteSpace(FormID))
                {
                    sb.Append(" AND FormID='" + FormID + "' ");
                }

                mo = base.DBSession.IView_Order_FormDAL.GetListBy("Select * from  View_Order_Form where FormState=1 " + sb.ToString() + "  order by LastTime desc ;");
                //    mo = base.DBSession.IOrder_Send_TDAL.GetListBy(u => u.LastUser == OperID && u.ConID == "101");
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.total = mo.Count;
            result.rows = mo.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return os.StringOfObject(result, RETID);
            #endregion
        }

        /// <summary>
        ///修改订单派送人员
        /// </summary>
        /// <param name="json">订单列表</param>
        /// <param name="tag">传（1）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string SetFormToTaker(string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            try
            {
                Model.Order_Send_T info = ParseClass<Model.Order_Send_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        //查询表中是否有这条数据，没有就增加，有就修改
                        int sentCount = base.GetListBy(u => u.FormID == info.FormID).Count;
                        if (sentCount <= 0)
                        {
                            #region 新增
                            T = "新增";
                            ret = base.Add(info);
                            int frmid = Convert.ToInt32(info.FormID);
                            // List<Model.Order_Form_T> mftli = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormID == frmid);
                            Model.Order_Form_T mft = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormID == frmid).First();
                            Model.Order_Form_T mft1 = new Model.Order_Form_T();
                            Help.FuZhi<Model.Order_Form_T>.FZ(ref mft1, mft);
                            Model.View_Order_Form ort = base.DBSession.IView_Order_FormDAL.GetListBy(u => u.FormID == frmid).First();
                            //Model.Order_Mer_T ort = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == mft.FormMerID).First();

                            mft1.Type = "200003";
                            mft1.FormMerID = ort.MerName;
                            mft1.IsEva = info.TakerID;
                            Help.ALL_DataV.PushQueue.Enqueue(mft1);

                            #region   新增日志
                            if (ret > 0)
                            {
                                // 将操作记录到日志表中
                                Model.Order_SendFolw_T Log = new Model.Order_SendFolw_T();  //订单状态变更日志
                                Log.FormID = info.FormID; //派送单id
                                Log.ConID = info.ConID; //派送单状态id
                                Log.LastUser = info.LastUser; //最后操作员
                                Log.LastTime = DateTime.Now.ToString("yyyyMMddHHmmss"); //最后操作时间
                                if (info.LastUser == 0) //等于0时为送餐员本人
                                {
                                    Log.BS = 1;
                                }
                                else
                                {
                                    Log.BS = 0;
                                }
                                int re = 0;
                                re = base.DBSession.IOrder_SendFolw_TDAL.Add(Log);
                                if (re < 0)
                                {
                                    WriteLog.WriteW("派送日志新增失败！");
                                }
                            }
                            #endregion
                            #endregion
                        }
                        else
                        {
                            #region 修改
                            T = "修改";
                            Model.Order_Send_T tmp = base.GetListBy(u => u.FormID == info.FormID).First();
                            //if (info.ConID == 107) //如果派送员拒绝接单，把派送单改回初始状态返回给调度员重新派送
                            //{
                            //    info.TakerID = null;
                            //    info.ConID = 101; //未指派状态
                            Model.Order_SendFolw_T Mosf = new Model.Order_SendFolw_T();  //日志派送
                            try
                            {
                                // Mosf = base.DBSession.IOrder_SendFolw_TDAL.GetListBy(u => u.FormID == info.FormID && u.ConID == 102).First();  //从流水查出派送单所属调度员
                                Mosf = base.DBSession.IOrder_SendFolw_TDAL.GetListBy("select * from  Order_SendFolw_T as a where FolwID=(SELECT MAX(FolwID) FROM  Order_SendFolw_T as b  where FormID=" + tmp.FormID + ")").First();//查询最后操作员ID
                            }
                            catch (Exception ex)
                            {
                                WriteLog.WriteW("反查调度员失败！");
                            }
                            info.LastUser = Mosf.LastUser;
                            //}

                            Help.FuZhi<Model.Order_Send_T>.FZ(ref tmp, info);
                            ret = base.Modify(tmp);

                            int formid = Convert.ToInt32(tmp.FormID);
                            Model.Order_Form_T mf = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormID == formid).First();
                            Model.Order_Form_T mf1 = new Model.Order_Form_T();
                            Help.FuZhi<Model.Order_Form_T>.FZ(ref mf1, mf);
                            Model.View_Order_Form or = base.DBSession.IView_Order_FormDAL.GetListBy(u => u.FormMerID == mf.FormMerID).First();

                            mf1.FormMerID = or.MerName;
                            mf1.IsEva = tmp.TakerID;
                            mf1.Type = "200003";
                            Help.ALL_DataV.PushQueue.Enqueue(mf1);

                            #region 状态变更-------新增派送日志
                            if (ret > 0)
                            {
                                // 将操作记录到日志表中
                                Model.Order_SendFolw_T Log = new Model.Order_SendFolw_T();  //订单状态变更日志
                                Log.FormID = info.FormID; //派送单id
                                Log.ConID = info.ConID; //派送单状态id
                                Log.LastUser = info.LastUser; //最后操作员
                                Log.LastTime = DateTime.Now.ToString("yyyyMMddHHmmss"); //最后操作时间
                                if (info.LastUser == 0) //等于0时为送餐员本人
                                {
                                    Log.BS = 1;
                                }
                                else
                                {
                                    Log.BS = 0;
                                }
                                int re = 0;
                                re = base.DBSession.IOrder_SendFolw_TDAL.Add(Log);
                                if (re < 0)
                                {
                                    WriteLog.WriteW("派送日志新增失败！");
                                }
                            }
                            #endregion
                            #endregion
                        }
                        break;
                    case 2:
                        #region 修改
                        //T = "修改";
                        //Model.Order_Send_T tmp = base.GetListBy(u => u.FormID == info.FormID).First();
                        ////if (info.ConID == 107) //如果派送员拒绝接单，把派送单改回初始状态返回给调度员重新派送
                        ////{
                        ////    info.TakerID = null;
                        ////    info.ConID = 101; //未指派状态
                        //Model.Order_SendFolw_T Mosf = new Model.Order_SendFolw_T();  //日志派送
                        //try
                        //{
                        //    // Mosf = base.DBSession.IOrder_SendFolw_TDAL.GetListBy(u => u.FormID == info.FormID && u.ConID == 102).First();  //从流水查出派送单所属调度员
                        //    Mosf = base.DBSession.IOrder_SendFolw_TDAL.GetListBy("select * from [ZHHQGXDX].[dbo].[Order_SendFolw_T] as a where FolwID=(SELECT MAX(FolwID) FROM [ZHHQGXDX].[dbo].[Order_SendFolw_T] as b  where FormID=" + info.FormID + ")").First();//查询最后操作员ID
                        //}
                        //catch (Exception ex)
                        //{
                        //    WriteLog.WriteW("反查调度员失败！");
                        //}
                        //info.LastUser = Mosf.LastUser;
                        ////}

                        //Help.FuZhi<Model.Order_Send_T>.FZ(ref tmp, info);
                        //ret = base.Modify(tmp);

                        //int formid = Convert.ToInt32(tmp.FormID);
                        //Model.Order_Form_T mf = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormID == formid).First();
                        //Model.Order_Mer_T or = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == mf.FormMerID).First();

                        //mf.FormMerID = or.MerName;
                        //mf.Type = "200003";
                        //Help.ALL_DataV.PushQueue.Enqueue(mf);

                        //#region 状态变更-------新增派送日志
                        //if (ret > 0)
                        //{
                        //    // 将操作记录到日志表中
                        //    Model.Order_SendFolw_T Log = new Model.Order_SendFolw_T();  //订单状态变更日志
                        //    Log.FormID = info.FormID; //派送单id
                        //    Log.ConID = info.ConID; //派送单状态id
                        //    Log.LastUser = info.LastUser; //最后操作员
                        //    Log.LastTime = info.LastTime; //最后操作时间
                        //    if (info.LastUser == 0) //等于0时为送餐员本人
                        //    {
                        //        Log.BS = 1;
                        //    }
                        //    else
                        //    {
                        //        Log.BS = 0;
                        //    }
                        //    int re = 0;
                        //    re = base.DBSession.IOrder_SendFolw_TDAL.Add(Log);
                        //    if (re < 0)
                        //    {
                        //        WriteLog.WriteW("派送日志新增失败！");
                        //    }
                        //}
                        //#endregion
                        #endregion
                        break;
                    case 3:
                        T = "删除";
                        List<Model.Order_Send_T> list_tmp = base.GetListBy(u => u.FormID == info.FormID);
                        Model.Order_Send_T st = new Model.Order_Send_T();
                        if (list_tmp != null && list_tmp.Count > 0)
                        {
                            st = list_tmp.First();
                            ret = base.Modify(st);

                            #region 删除操作------新增日志
                            if (ret > 0)
                            {
                                // 将操作记录到日志表中
                                Model.Order_SendFolw_T Log = new Model.Order_SendFolw_T();  //订单状态变更日志
                                Log.FormID = info.FormID; //派送单id
                                Log.ConID = info.ConID; //派送单状态id
                                Log.LastUser = info.LastUser; //最后操作员
                                Log.LastTime = DateTime.Now.ToString("yyyyMMddHHmmss"); //最后操作时间
                                if (info.LastUser == 0) //等于0时为送餐员本人
                                {
                                    Log.BS = 1;
                                }
                                else
                                {
                                    Log.BS = 0;
                                }
                                int re = 0;

                                re = base.DBSession.IOrder_SendFolw_TDAL.Add(Log);
                                if (re < 0)
                                {
                                    WriteLog.WriteW("派送日志新增失败！");
                                }
                            }
                            #endregion
                        }
                        break;
                }

                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                    WriteLog.WriteW("变更派送单状态失败");
                }
            }
            catch (DbEntityValidationException dbex)
            {
                StringBuilder errors = new StringBuilder();
                IEnumerable<DbEntityValidationResult> validationResult = dbex.EntityValidationErrors;
                foreach (DbEntityValidationResult result in validationResult)
                {
                    ICollection<DbValidationError> validationError = result.ValidationErrors;
                    foreach (DbValidationError err in validationError)
                    {
                        errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
                    }
                }
                //Console.WriteLine(errors.ToString());
                WriteLog.WriteW("AddOrder_OperationFlow异常：" + errors.ToString());

            }
            catch (DbUpdateException ex)
            {
                WriteLog.WriteW("下订单信息失败DbUpdateException：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统异常";
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新派送信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion

            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 统计列表
        /// </summary>
        /// <param name="OperID">调度员id</param>
        /// <param name="OpenID">派送员微信id</param>
        /// <param name="pageIndex">起始页</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="sdate">开始时间</param>
        /// <param name="edate">结束时间</param>
        /// <param name="ConID">状态</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetOrder_SendList(string OperID, string OpenID, int pageIndex, int pageSize, string StarTime, string EndTime, int ConID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                RS.BS = "-88";
                RS.Msg = "验证码错误！";
                return os.StringOfObject(RS, RETID);
            }
            #endregion

            #region 执行操作
            try
            {
                StringBuilder sql_a = new StringBuilder();
                StringBuilder sql_b = new StringBuilder();
                StringBuilder sql_c = new StringBuilder();

                sql_a.Append("SELECT  * FROM View_Order_Send_T where TakerOpenID=" + "'" + OpenID + "'" + " and OperID=" + OperID + ""); //拼接搜索条件
                if (StarTime != "")
                {
                    sql_a.Append(" and LastTime >= '" + StarTime + "' ");  //开始时间
                }
                if (EndTime != "")
                {
                    sql_a.Append(" and LastTime <= '" + EndTime + "' ");  //结束时间
                }
                if (ConID.ToString() != "" && ConID != 0)  //0是统计所有
                {
                    sql_a.Append(" and ConID=" + ConID + ""); //状态
                }
                else if (ConID.ToString() != "" && ConID == 0)
                {
                    sql_a.Append(" and (ConID=105 or ConID=106) ");
                }
                sql_b.Append(" order by LastTime desc");
                string a = sql_a.ToString() + sql_b.ToString();
                List<Model.View_Order_Send_T> ls = base.DBSession.IView_Order_Send_TDAL.GetListBy(a); //查询总数
                sql_c.Append("and ConID=106"); //派送停止
                List<Model.View_Order_Send_T> ls_stop = base.DBSession.IView_Order_Send_TDAL.GetListBy(sql_a.ToString() + sql_c.ToString()); //派送停止
                if (ls_stop != null && ls.Count > 0 && ls != null && ls.Count > 0)
                {
                    RS.Stop = ls_stop.Count; //派送停止数量
                    RS.End = ls.Count - ls_stop.Count; //派送完成数量
                }
                else if (ls_stop.Count == 0 && ls != null && ls.Count > 0)
                {
                    RS.Stop = 0;
                    RS.End = ls.Count;
                }
                if (ls != null && ls.Count > 0)
                {
                    RS.BS = "1";
                    RS.Msg = "查询成功！";
                    RS.total = ls.Count;
                    RS.rows = ls.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    RS.BS = "0";
                    RS.Msg = "查询不到数据";
                }
                return os.StringOfObject(RS, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("查询金额流水失败：" + ex.Message);
                RS.BS = "-99";
                RS.Msg = "系统错误！";
                return os.StringOfObject(RS, RETID);
            }

            #endregion
        }

        /// <summary>
        /// 属于派送员（正在派送和未接单）的单子
        /// </summary>
        /// <param name="TakerOpenID">微信id</param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetOrder_SendTaker(string TakerOpenID, int pageIndex, int pageSize, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            List<Model.Order_Taker_T> ot = new List<Model.Order_Taker_T>();
            List<Model.View_Order_Send_T> mo = new List<Model.View_Order_Send_T>();
            try
            {
                ot = base.DBSession.IOrder_Taker_TDAL.GetListBy(u => u.TakerOpenID == TakerOpenID && u.TakerState == 1);  //根据入参的微信id查出派送员的信息
                if (ot.Count != 0)
                {
                    try
                    {
                        int takerid = ot.First().TakerID;
                        mo = base.DBSession.IView_Order_Send_TDAL.GetListBy(u => u.TakerID == takerid && (u.ConID == "102" || u.ConID == "103" || u.ConID == "104")); //查询该派送员未完成的派送单
                    }
                    catch (Exception ex)
                    {
                        result.BS = "-1";
                        result.Msg = "查询不到数据！";
                        return os.StringOfObject(result, RETID);
                    }
                }
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }

            result.BS = "1";
            result.Msg = "查询成功！";
            result.total = mo.Count;
            result.rows = mo.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            return os.StringOfObject(result, RETID);
            #endregion
        }

        /// <summary>
        /// 当前派送单状态的下一个派送单状态
        /// </summary>
        /// <param name="FormID"></param>
        /// <param name="ConID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns>Order_FormControl_T</returns>
        public string GetNextCon(string FormID, string ConID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            int form = int.Parse(FormID);
            List<Model.View_Order_Send_T> ot = new List<Model.View_Order_Send_T>();
            Model.Order_FormControl_T Mof = new Model.Order_FormControl_T();
            List<Model.Order_FormControl_T> of = new List<Model.Order_FormControl_T>();
            List<Model.Order_FormControl_T> newof = new List<Model.Order_FormControl_T>(); // return list
            try
            {
                ot = base.DBSession.IView_Order_Send_TDAL.GetListBy(u => u.FormID == form && u.SendState == 1);  //根据订单号查询
                if (ot.Count != 0)
                {
                    try
                    {
                        string conid = ot.First().ConID.ToString();  //当前订单状态
                        of = base.DBSession.IOrder_FormControl_TDAL.GetListBy(u => u.ConID == conid && u.ConState == 1);  //查询订单控制表，获取下一个订单状态
                        if (of.Count > 0)
                        {
                            string nextcon = of.First().NextCon;
                            string[] arr = nextcon.Split(',');
                            if (arr.Length > 1) //如果下一状态存在多个
                            {
                                for (int i = 0; i < arr.Length; i++) //遍历所有下个状态
                                {
                                    Mof = base.DBSession.IOrder_FormControl_TDAL.GetListBy(u => u.ConID == arr[i]).First();
                                    newof.Add(Mof);
                                }

                            }
                            else //下一状态只有一个
                            {
                                //int newnextcon = int.Parse(nextcon);
                                newof = base.DBSession.IOrder_FormControl_TDAL.GetListBy(u => u.ConID == nextcon);
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        result.BS = "-1";
                        result.Msg = "查询不到数据！";
                        return os.StringOfObject(result, RETID);
                    }
                }
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }

            result.BS = "1";
            result.Msg = "查询成功！";
            result.total = newof.Count;
            result.rows = newof;

            return os.StringOfObject(result, RETID);
            #endregion

        }

        /// <summary>
        /// 获取订单状态控制
        /// </summary>
        /// <returns></returns>
        public string GetCon(string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            List<Model.Order_FormControl_T> ls = new List<Model.Order_FormControl_T>();
            try
            {
                ls = base.DBSession.IOrder_FormControl_TDAL.GetListBy(u => u.ConState == 1);
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.total = ls.Count;
            result.rows = ls;

            return os.StringOfObject(result, RETID);
            #endregion

        }

        /// <summary>
        /// 查询派送单
        /// </summary>
        /// <param name="OperID">操作员id</param>
        /// <param name="ConID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string SearchSend(int OperID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

            #region 执行操作
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT  * FROM View_Order_Send_T where OperID=" + OperID + ""); //拼接搜索条件
                sql.Append(" order by LastTime desc");
                List<Model.View_Order_Send_T> ls = base.DBSession.IView_Order_Send_TDAL.GetListBy(sql.ToString()); //查询总数
                if (ls != null && ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功！";
                    result.total = ls.Count;
                    result.rows = ls;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("查询派送单失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
        }
        /// <summary>
        /// 查询该商户id下所有或个别派送员订单的派送
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="Merid">商户ID</param>
        /// <param name="TakerName">配送员姓名</param>
        /// <param name="FormID">订单ID</param>
        /// <param name="Stime">开始时间（yyyyMMdd）</param>
        /// <param name="Etime">结束时间（yyyyMMdd）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetGrid_Wiew_Send(int pageIndex, int pageSize, string Merid, string TakerName, string FormID, string Stime, string Etime, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * FROM View_Order_Send_T WHERE MerID=" + Merid + " "); //拼接搜索条件

                if (TakerName != null && TakerName != "")
                {
                    sql.Append("AND TakerName like '%" + TakerName + "%' ");
                }
                if (FormID != null && FormID != "")
                {
                    sql.Append("AND FormID=" + FormID + " ");
                }
                if (Stime != null && Stime != "")
                {
                    sql.Append("AND SenTime>='" + Stime + "000000' ");
                }
                if (Etime != null && Etime != "")
                {
                    sql.Append("AND SenTime<='" + Etime + "235959'");
                }
                List<Model.View_Order_Send_T> mo = base.DBSession.IView_Order_Send_TDAL.GetListBy(sql.ToString());
                if (mo != null && mo.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功！";
                    result.total = mo.Count;
                    result.rows = mo.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);

            }
            catch (Exception ex)
            {
                WriteLog.WriteW("查询派送员派送订单失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

        }
        /// <summary>
        /// 查询该订单id下对应个别派送员订单的派送
        /// </summary>
        /// <param name="FormID">订单ID</param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string GetGrid_Wiew( string FormID  ,string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                List<Model.Order_Form_T> mo = base.DBSession.IOrder_Form_TDAL.GetListBy("SELECT * FROM Order_Form_T WHERE FormID=" + FormID + " ");
                Model.Order_Form_T a = mo[0];
                if (mo != null && mo.Count > 0)
                {
                    List<Send_Form> qq = new List<Send_Form> ();
                    Send_Form moo = new Send_Form();
                    Model.Order_Mer_T mer = base.DBSession.IOrder_Mer_TDAL.GetListBy("SELECT * FROM Order_Mer_T WHERE MerID=" + a.FormMerID + " ").First();
                    string B=a.FormBY.Substring(1,1);
                    if(B=="B"){
                        Model.View_Order_Send_T taker = base.DBSession.IView_Order_Send_TDAL.GetListBy("SELECT * FROM View_Order_Send_T  WHERE FormID=" + FormID + " ").First();
                        moo.BZ = taker.BZ;
                        moo.SendState = taker.SendState;
                        moo.TakerName = taker.TakerName;
                        moo.TakerPhone = taker.TakerPhone;
                        moo.TakerOpenID = taker.TakerOpenID;
                        moo.ConText = taker.ConText;
                        moo.ConIsOut = taker.ConIsOut;
                        moo.NextCon = taker.NextCon;
                        moo.ConID = taker.ConID.ToString();
                        moo.TakerID = taker.TakerID;
                        moo.LastUser = taker.LastUser;
                    }
                    
                    moo.LastTime = a.LastTime;
                    moo.FormBY = a.FormBY;
                    moo.FormTime = a.FormTime;
                    moo.FormID = a.FormID;
                    moo.SenTime = a.SenTime;
                    moo.FormAddress = a.FormAddress;
                    moo.AccName = a.AccName;
                    moo.AccID = a.AccID;
                    moo.AccCall = a.AccCall;
                    moo.MerID = mer.MerID;
                    moo.MerCall = mer.MerCall;
                    moo.MerName = mer.MerName;
                    qq.Add(moo);
                    result.BS = "1";
                    result.Msg = "查询成功！";
                    result.total = qq.Count;
                    result.rows = qq;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);

            }
            catch (Exception ex)
            {
                WriteLog.WriteW("查询派送员派送订单失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion

        }
        public class Send_Form{
            public int LastUser { get; set; }
            public string LastTime { get; set; }
            public string BZ { get; set; }
            public int SendState { get; set; }
            public string TakerName { get; set; }
            public string TakerPhone { get; set; }
            public string TakerOpenID { get; set; }
            
            public string ConText { get; set; }
            public int ConIsOut { get; set; }
            public string NextCon { get; set; }
            public string ConID { get; set; }
            public int TakerID { get; set; }
            public int FormID { get; set; }
            public string SenTime { get; set; }

            public string FormTime { get; set; }//下单时间
            public string FormBY { get; set; }//订单状态
            public string FormAddress { get; set; }
            public string AccName { get; set; }
            public int AccID { get; set; }
            public string AccCall { get; set; } 

            public string MerID { get; set; }
            public string MerName { get; set; }
            public string MerCall { get; set; }
        }

    }
}
